package ro.upt.dcda.pricezone.s2.requests;

import javax.jms.JMSException;
import javax.jms.StreamMessage;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ro.upt.dcda.pricezone.common.ConnectionFactory;
import ro.upt.dcda.pricezone.s2.pojo.ProductPrice;

public class NewProductInfoRequest implements S2Request {

	private int storeID;
	private int productID;
	private String price;
	private String currency;

	public NewProductInfoRequest(StreamMessage request) throws JMSException {
		storeID = request.readInt();
		productID = request.readInt();
		price = request.readString();
		currency = request.readString();
	}

	public void work(StreamMessage message) throws JMSException {
		Session session = ConnectionFactory.getFactory().openSession();
		try {
			Transaction tx = session.beginTransaction();
			ProductPrice info = new ProductPrice();
			info.setStore_id(storeID);
			info.setProduct_id(productID);
			info.setPrice(price);
			info.setCurrency(currency);
			session.save(info);
			tx.commit();
			message.writeInt(1);
		} catch (HibernateException e) {
			e.printStackTrace();
			message.writeInt(0);
		} finally {
			session.close();
		}
	}
}
